import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { RoashProgressBar } from "../view/fishing/roastFish/RoashProgressBar";
import { ItemData } from "../resource/info/ItemData";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { IFishModule } from "../../../modules/interfaces/IFishModule";
import { ClockManager } from "../manager/ClockManager";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { IView } from "../../mvc/interfaces/IView";
import { FishModule } from "../../../modules/FishModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { FishDropItemWindow } from "../view/fishing/FishDropItemWindow";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { Game } from "../Game";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { SPoint } from "../../../Message/Public/SPoint";
import { GameProxy } from "../mvc/GameProxy";
import { FishingResultDef } from "../view/fishing/defin/FishingResultDef";
import { FishPickDef } from "../view/fishing/defin/FishPickDef";
import { SkillInfo } from "../model/SkillInfo";
import { LayerManager } from "../manager/LayerManager";
import { GameConst } from "../../component/gconst/GameConst";
type int = number;
//class FishController
    
    export  class FishController extends Controller
    {
       

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (this._isInGuildScene && !GameMapUtil.isGuildMap()) 
            {
                this._isInGuildScene = false;
                if (RoashProgressBar.isInitInstance) 
                {
                    RoashProgressBar.instance.onEndRoastFish();
                }
            }
            if (!this._isInGuildScene && GameMapUtil.isGuildMap()) 
            {
                this._isInGuildScene = true;
            }
            return;
        }

        /* internal  */onStartRoastFishSuccess(arg1: ItemData): void
        {
            this._roastFishItemData = arg1;
            this._isHaveShowRoastFish = true;
            RoashProgressBar.instance.startRoash();
            RoashProgressBar.instance.updateTimers(cache.fish.roastTime, cache.fish.roastMaxTime);
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.FishGotoFish, this.onFishGotoFish);
            Dispatcher.addEventListener(EventName.FishStartFish, this.onStartFish);
            Dispatcher.addEventListener(EventName.FishEndFish, this.onEndFish);
            Dispatcher.addEventListener(EventName.FishFishOne, this.onFishOne);
            Dispatcher.addEventListener(EventName.FishPickItem, this.onFishPickItem);
            Dispatcher.addEventListener(EventName.FishRefreshFishItemList, this.onFishUpdateList);
            NetDispatcher.addCmdListener(ServerCommand.SkillFishLeared, this.onSkillFishLeared);
            NetDispatcher.addCmdListener(ServerCommand.FishStartFish, this.onStartFishSuccess);
            NetDispatcher.addCmdListener(ServerCommand.FishEndFish, this.onEndFishSuccess);
            NetDispatcher.addCmdListener(ServerCommand.FishNumChange, this.onFishNumChange);
            NetDispatcher.addCmdListener(ServerCommand.FishItemsUpdate, this.onFishItemsUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ActiveTypeFishHook, this.onActiveTypeFishHook);
            Dispatcher.addEventListener(EventName.StartRoastFish, this.onStartRoastFish);
            Dispatcher.addEventListener(EventName.EndRoastFish, this.onEndRoastFish);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            NetDispatcher.addCmdListener(ServerCommand.FishStartRoastFish, this.onStartRoastFishSuccess);
            NetDispatcher.addCmdListener(ServerCommand.FishEndRoastFish, this.onEndRoastFishSuccess);
            return;
        }

        /* internal  */onActiveTypeFishHook(arg1: Object): void
        {
            var loc1=arg1 as SActiveToClient;
            var loc2=loc1.code;
            if (view && !view.isHide) 
            {
                (view as IFishModule).updateActiveUI(loc2);
                ClockManager.instance.addDateCall(loc1.endDt, this.removeActive);
            }
            return;
        }

        /* internal  */removeActive(): void
        {
            if (view && !view.isHide) 
            {
                (view as IFishModule).updateActiveUI(EActiveType._EActiveTypeSuperFishHook);
            }
            return;
        }

        /* internal  */onEndRoastFishSuccess(arg1: Object): void
        {
            RoashProgressBar.instance.hide();
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._fishModule) 
            {
                this._fishModule = new FishModule();
                this._fishModule.addEventListener(WindowEvent.CLOSE, this.onCloseWindow);
                this._fishModule.addEventListener(WindowEvent.SHOW, this.onShowWindow);
                this._isCreateFishView = true;
            }
            return this._fishModule;
        }

        /* internal  */get fishDropItemWindow(): FishDropItemWindow
        {
            if (!this._fishDropItemWindow) 
            {
                this._fishDropItemWindow = FishDropItemWindow.instance;
            }
            return this._fishDropItemWindow;
        }

        public isViewHide(): Boolean
        {
            if (!_view || _view.isHide) 
            {
                return true;
            }
            return false;
        }

        /* internal  */onCloseWindow(arg1: WindowEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.FishEndFish));
            NetDispatcher.removeCmdListener(ServerCommand.BackpackDataChange, this.onPackChange);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.onPackChange);
            return;
        }

        /* internal  */onShowWindow(arg1: WindowEvent): void
        {
            this.onPackChange();
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onPackChange);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChange);
            return;
        }

        public get isHaveShowRoastFish(): Boolean
        {
            return this._isHaveShowRoastFish;
        }

        /* internal  */onFishGotoFish(arg1: DataEvent): void
        {
            if (RolePlayer.instance.isFishing) 
            {
                return;
            }
            if (RolePlayer.instance.fighting) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41500));
                return;
            }
            if (RolePlayer.instance.isTurned) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41501));
                return;
            }
            if (cache.battle.isInBattle) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41502));
                return;
            }
            this.fishPoint = Game.sceneInfo.getFishingNearPoint();
            if (!this.fishPoint || cache.scene.isTaijiScene()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41503));
                return;
            }
            AIManager.onAIControl(this.fishPoint, AIType.AI_Fish);
            return;
        }

        /* internal  */onStartFish(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPoint;
            GameProxy.fish.startFish(loc1);
            return;
        }

        /* internal  */onEndFish(arg1: DataEvent): void
        {
            if (cache.fish.isInFishing) 
            {
                GameProxy.fish.endFish();
            }
            return;
        }

        /* internal  */onFishOne(arg1: DataEvent): void
        {
            var loc1=arg1.data as FishingResultDef;
            GameProxy.fish.fish(loc1.uid, loc1.isHookFish);
            return;
        }

        /* internal  */onFishPickItem(arg1: DataEvent): void
        {
            var loc1=arg1.data as FishPickDef;
            GameProxy.fish.pickFishItem(loc1.uid, loc1.isPickAll);
            return;
        }

        /* internal  */onSkillFishLeared(arg1: SkillInfo): void
        {
            MsgManager.showRollTipsMsg(Language.getString(41504));
            var loc1=cache.skill.getBaseSkillById(FishController.fishSkillId);
            Dispatcher.dispatchEvent(new DataEvent(EventName.AddToShortcutsByReq, loc1));
            return;
        }

        /* internal  */onStartFishSuccess(arg1: Object): void
        {
            view.show();
            return;
        }

        /* internal  */onEndFishSuccess(arg1: Object): void
        {
            view.hide();
            var loc1=cache.pack.fishPackCache.getItemsByJudge();
            if (loc1.length > 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41505));
                this.fishDropItemWindow.show();
                this.onFishUpdateList();
                LayerManager.windowLayer.centerPopup(this.fishDropItemWindow);
            }
            else 
            {
                this.fishDropItemWindow.hide();
            }
            return;
        }

        /* internal  */onFishUpdateList(arg1: Object=null): void
        {
            var loc1=cache.pack.fishPackCache.getItemsByJudge();
            FishDropItemWindow.instance.updateData(loc1);
            return;
        }

        /* internal  */onFishNumChange(arg1: Object): void
        {
            var loc1=GameConst.FishMaxCount;
            if (cache.active.isActiveInEffect(EActiveType._EActiveTypeFishExtraCount)) 
            {
                loc1 = GameConst.FishMaxCountEx;
            }
            (view as IFishModule).updateFishNum(cache.fish.fishNum, loc1);
            return;
        }

        /* internal  */onFishItemsUpdate(arg1: Object): void
        {
            var loc1=null;
            if (this._isCreateFishView) 
            {
                loc1 = cache.pack.fishPackCache.getItemsByJudge();
                this.fishDropItemWindow.updateData(loc1);
                if (loc1.length > 0) 
                {
                    (view as IFishModule).flicker();
                }
                else 
                {
                    (view as IFishModule).stopFlicker();
                    if (!cache.fish.isInFishing) 
                    {
                        this.fishDropItemWindow.hide();
                    }
                }
            }
            return;
        }

        /* internal  */onPackChange(arg1: Object=null): void
        {
            (view as IFishModule).resetHookMsg();
            return;
        }

        /* internal  */onStartRoastFish(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            GameProxy.fish.startRoastFish(loc1);
            return;
        }

        /* internal  */onEndRoastFish(arg1: DataEvent): void
        {
            GameProxy.fish.endRoastFish(this._roastFishItemData.uid);
            return;
        }

        private static /* const */fishSkillId: int=2000003;

        private /* var */_fishModule: IFishModule;

        private /* var */_fishDropItemWindow: FishDropItemWindow;

        private /* var */_isCreateFishView: Boolean=false;

        private /* var */fishPoint: Object /* flash.geom.Point */;

        private /* var */_roastFishItemData: ItemData;

        private /* var */_isInGuildScene: Boolean=false;

        private /* var */_isHaveShowRoastFish: Boolean=false;
    }
